package com.blt.car.base;

import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/**
 * Created by xgp on 2017/6/11.
 */

public class ViewHolder {
    private SparseArray<View> mViews;
    private View mRootView;

    public ViewHolder(LayoutInflater inflater, ViewGroup parent, int layoutId) {
        mViews = new SparseArray<>();
        mRootView = inflater.inflate(layoutId, parent, false);
    }

    public <T extends View> T get(int resId) {
        View view = mViews.get(resId);
        if (view == null) {
            view = mRootView.findViewById(resId);
            mViews.put(resId, view);
        }
        return (T) view;
    }

    public View getmRootView() {
        return mRootView;
    }

    public void setOnClickListener(View.OnClickListener clickListener, int... resIds) {
        if (resIds == null) {
            return;
        }

        for (int id : resIds) {
            get(id).setOnClickListener(clickListener);
        }
    }


}
